package com.spark.rectify

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.ImageView
import com.blankj.utilcode.util.LogUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.spark.common.BaseFragment
import com.spark.common.ext.server
import kotlinx.android.synthetic.main.frag_rectify_manage.*

/**
 * 纠违管理
 */
class RectifyManageFragment : BaseFragment() {
    private lateinit var rectifyManageAdapter: RectifyManageAdapter

    companion object {
        fun newInstance(): RectifyManageFragment {
            val target = RectifyManageFragment()
            val args = Bundle()

            target.arguments = args
            return target
        }
    }

    override fun contentResId(): Int {
        return R.layout.frag_rectify_manage
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        iv_back.setOnClickListener { pop() }
        tv_title.text = "纠违管理"
        iv_category.setOnClickListener {
            val dfRectifManage = DFRectManage.newInstance()
            dfRectifManage.showNow(childFragmentManager, "DFRectifyManage")
            dfRectifManage.addFilterListener {
                LogUtils.e(it)
            }
        }
        refresh_layout.apply {
            setOnRefreshListener { }
            setOnLoadmoreListener { }
        }
        recy_hts.apply {
            rectifyManageAdapter = RectifyManageAdapter(R.layout.item_rectify_manage).apply {
                setOnItemClickListener { adapter, view, position ->
                    val model = rectifyManageAdapter.data[position]
                    activity?.apply { RectManageProcedureActivity.actionStart(this, Gson().toJson(model)) }
                }
            }
            adapter = rectifyManageAdapter
            layoutManager = LinearLayoutManager(context)
        }
    }
}

data class RectifyManageListModel(
        var id: String = "",
        var avatar: String = "",
        var name: String = "",
        var unit: String = "",
        var leader: String = "",
        var state: String = "",
        var desc: String = "",
        var time: String = ""
)

class RectifyManageAdapter(layoutRes: Int) : BaseQuickAdapter<RectifyManageListModel, BaseViewHolder>(layoutRes) {
    override fun convert(helper: BaseViewHolder?, item: RectifyManageListModel?) {
        if (item == null) return
        helper?.apply {
            if (!item.avatar.isNullOrBlank()) getView<ImageView>(R.id.civ_avatar).server(item.avatar)
            setText(R.id.tv_user_info, item.name)
            setText(R.id.tv_leader, item.leader)
            setText(R.id.tv_state, item.state)
            setText(R.id.tv_desc, item.desc)
            setText(R.id.tv_time, item.time)
        }
    }

}